#include "config.h"
#include "myFunctions.h"
#include "keyaction.h"
#include "web.h"
#include <ESP8266WiFi.h>
const unsigned long BUTTON_PRESS_DURATION = 3000; // 需要按住3秒
//开机重启时候长安按钮1检查启动时按钮是否按下
void ICACHE_FLASH_ATTR checkButtonPress() {
  Serial.println("检查按钮状态...");
  if (digitalRead(CONFIG_BUTTON) == LOW) {
    Serial.println("检测到按钮按下，等待3秒确认...");
    buttonPressStartTime = millis();

    while (millis() - buttonPressStartTime < BUTTON_PRESS_DURATION) {
      if (digitalRead(CONFIG_BUTTON) == HIGH) {
        Serial.println("按钮释放，取消配网模式");
        return;
      }
      delay(100);
      Serial.print(".");
    }

    buttonPressedDuringBoot = true;
    Serial.println("\n按钮按住3秒，进入配网模式");
  }
}
void setupButton() {
  pinMode(CONFIG_BUTTON, INPUT_PULLUP);
  pinMode(KEY1_BUTTON, INPUT_PULLUP);
  pinMode(KEY2_BUTTON, INPUT_PULLUP);
  pinMode(KEY3_BUTTON, INPUT_PULLUP);
  pinMode(TEST_BUTTON, INPUT_PULLUP);
}

// 检查运行时按钮按下
void ICACHE_FLASH_ATTR checkRuntimeButton() {
  static unsigned long lastCheck = 0;
  static bool lastButtonState = HIGH;
  if (millis() - lastCheck > 100) {
    bool currentState = digitalRead(CONFIG_BUTTON);

    if (currentState == LOW && lastButtonState == HIGH) {
      // 按钮按下，开始计时
      buttonPressStartTime = millis();
    }

    if (currentState == LOW &&
        millis() - buttonPressStartTime > BUTTON_PRESS_DURATION &&
        !isConfigMode) {
      Serial.println("运行时长按按钮，进入配网模式");
      enterConfigMode();
    }
    if (currentState == LOW &&
        millis() - buttonPressStartTime > BUTTON_PRESS_DURATION &&
        isConfigMode) {
      Serial.println("退出配网模式");
      ESP.restart();
      isConfigMode = false;
    }
    lastButtonState = currentState;
    lastCheck = millis();
  }
}

// 检查运行时按钮按下
void ICACHE_FLASH_ATTR checKeyButton() {
  static bool key1state = false;
  static bool key2state = false;
  static bool key3state = false;
  static bool key4state = false;
  if (digitalRead(KEY1_BUTTON) == LOW && !key3state) {
    key1state = true;
    delay(100);
    Serial.print(".");
  }

  if (digitalRead(KEY2_BUTTON) == LOW && !key2state) {
    key2state = true;
    delay(100);
    Serial.print(".");
  }
  if (digitalRead(KEY3_BUTTON) == LOW && !key3state) {
    key3state = true;
    delay(100);
    Serial.print(".");
  }
  if (digitalRead(TEST_BUTTON) == LOW && !key4state) {
    key4state = true;
    delay(100);
    Serial.print(".");
  }
  ////////////////////////////三个按钮的功能//////////////////////////////
  if (key1state && digitalRead(KEY1_BUTTON) == HIGH) {
    Serial.println("按钮1");

    presskey(webConfig.k1, 0);
    // printStoredIP();
    key1state = false;
    return;
  }
  if (key2state && digitalRead(KEY2_BUTTON) == HIGH) {
    Serial.println("按钮2");
    //JsonDocument result = RequestSendKey(createRequestBody(webConfig.k2, "key"), "https://api.gzqizhen.com/api/esp/getkey");
    printUserConfig();
    printDeviceInfo();
    printWebConfig();
    printwifiConfig();
    key2state = false;
    return;
  }
  if (key3state && digitalRead(KEY3_BUTTON) == HIGH) {
    Serial.println("按钮3");
    presskey(webConfig.k3, 0);
    key3state = false;
    return;
  }
  if (key4state && digitalRead(TEST_BUTTON) == HIGH) {
    Serial.println("测试按钮");
    clearUserConfig();
    key4state = false;
    return;
  }
}
